home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Symantec Visual Cafe for Java 2.5
/
symantec-visual-cafe-2.5-database-dev-edition.iso
/
Visual Cafe Pro v1.0
/
SOURCE.BIN
/
BorderPanel.java
< prev
next >
Encoding:
Amiga
Atari
Commodore
DOS
FM Towns/JPY
Macintosh
Macintosh JP
Macintosh to JP
NeXTSTEP
RISC OS/Acorn
Shift JIS
UTF-8
Wrap
Java Source
|
1997-06-19
|
27.9 KB
|
1,071 lines
package symantec.itools.awt;
import java.awt.Panel;
import java.awt.Insets;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Font;
// 01/29/97 TWB Integrated changes from Macintosh
/**
* BorderPanel is a panel component that has an optional border
* and optional text title.
* @version 1.0, Nov 26, 1996
* @author Symantec
*/
public class BorderPanel
extends Panel
implements AlignStyle,
BevelStyle
{
/**
* Label x-coordinate padding constant.
*/
protected static final int labelpadx = 10;
/**
* Label x-coordinate inset constant.
*/
protected static final int labelipadx = 4;
/**
* The border color.
*/
protected Color borderColor;
/**
* The border label color.
*/
protected Color labelColor;
/**
* The top border padding amount.
*/
protected int padtop;
/**
* The bottom border padding amount.
*/
protected int padbottom;
/**
* The left border padding amount.
*/
protected int padleft;
/**
* The right border padding amount.
*/
protected int padright;
/**
* The side inset border padding amount.
*/
protected int ixPad;
/**
* The top inset border padding amount.
*/
protected int iyPadTop;
/**
* The bottom inset border padding amount.
*/
protected int iyPadBottom;
/**
* The border style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, BEVEL_NONE.
* @see BevelStyle#BEVEL_RAISED
* @see BevelStyle#BEVEL_LOWERED
* @see BevelStyle#BEVEL_LINE
* @see BevelStyle#BEVEL_NONE
*/
protected int style;
/**
* The current border label.
*/
protected String label;
/**
* The border label alignment: ALIGN_LEFT, ALIGN_CENTERED, ALIGN_RIGHT.
* @see AlignStyle#ALIGN_LEFT
* @see AlignStyle#ALIGN_CENTERED
* @see AlignStyle#ALIGN_RIGHT
*/
protected int labelAlignment;
/**
* The internal border insets.
*/
protected Insets internalInsets;
/**
* The panel within the border.
*/
protected Panel panel;
/**
* The overall size before sizepanel().
*/
Dimension oldSize = new Dimension();
/**
* Constructs a new default BorderPanel.
*/
public BorderPanel()
{
this(null, ALIGN_CENTERED, BEVEL_LINE);
}
/**
* Constructs a new default BorderPanel with the specified beveled border style.
* It is constructed with the ALIGN_CENTERED label alignment style.
* @param style desired beveled border style
* @see BevelStyle#BEVEL_RAISED
* @see BevelStyle#BEVEL_LOWERED
* @see BevelStyle#BEVEL_LINE
* @see BevelStyle#BEVEL_NONE
*/
public BorderPanel(int style)
{
this(null, ALIGN_CENTERED, style);
}
/**
* Constructs a new default BorderPanel with the specified border label.
* It is constructed with the ALIGN_CENTERED and BEVEL_LINE styles.
*/
public BorderPanel(String s)
{
this(s, ALIGN_CENTERED, BEVEL_LINE);
}
/**
* Constructs a new default BorderPanel with the specified border label
* and given alignment.
* @param label border panel label string
* @param alignment alignment of label string
* @see AlignStyle#ALIGN_LEFT
* @see AlignStyle#ALIGN_CENTERED
* @see AlignStyle#ALIGN_RIGHT
*/
public BorderPanel(String s, int alignment)
{
this(s, alignment, BEVEL_LINE);
}
/**
* Constructs a new default BorderPanel with the specified border label
* and given alignment and border styles.
* @param label border panel label string
* @param alignment alignment of label string
* @param style border style
* @see AlignStyle#ALIGN_LEFT
* @see AlignStyle#ALIGN_CENTERED
* @see AlignStyle#ALIGN_RIGHT
* @see BevelStyle#BEVEL_RAISED
* @see BevelStyle#BEVEL_LOWERED
* @see BevelStyle#BEVEL_LINE
* @see BevelStyle#BEVEL_NONE
*/
public BorderPanel(String s, int alignment, int style)
{
borderColor = Color.black;
labelColor = Color.black;
padleft = 6;
padright = 6;
padtop = 10;
padbottom = 6;
ixPad = 4;
iyPadTop = 2;
iyPadBottom = 7;
label = (s != null && s.length() == 0) ? null : s;
labelAlignment = alignment;
internalInsets = new Insets(10, 10, 10, 10);
this.style = style;
setLayout(null);
super.add(panel = new Panel());
sizepanel(true);
}
/**
* Tells this component that it has been added to a container.
* This is a standard Java AWT method which gets called by the AWT when
* this component is added to a container. Typically, it is used to
* create this component's peer.
* It is overridden here to resize self after peer created.
*
* @see java.awt.Container#removeNotify
*/
public void addNotify()
{
super.addNotify();
sizepanel(true);
}
/**
* Sets the top border padding amount.
* @param p top border pad amount
* @see #getPaddingTop
*/
public void setPaddingTop(int p)
{
padtop = p;
sizepanel(true);
invalidate();
}
/**
* Gets the current top border pad amount.
* @return top border pad amount
* @see #setPaddingTop
*/
public int getPaddingTop()
{
return padtop;
}
/**
* Sets the bottom border padding amount.
* @param p bottom border pad amount
* @see #getPaddingBottom
*/
public void setPaddingBottom(int p)
{
padbottom = p;
sizepanel(true);
invalidate();
}
/**
* Gets the current bottom border pad amount.
* @return bottom border pad amount
* @see #setPaddingBottom
*/
public int getPaddingBottom()
{
return padbottom;
}
/**
* Sets the left border padding amount.
* @param p left border pad amount
* @see #getPaddingLeft
*/
public void setPaddingLeft(int p)
{
padleft = p;
sizepanel(true);
invalidate();
}
/**
* Gets the current left border pad amount.
* @return int - left border pad amount
* @see #setPaddingLeft
*/
public int getPaddingLeft()
{
return padleft;
}
/**
* Sets the right border padding amount.
* @param p right border pad amount
* @see #getPaddingRight
*/
public void setPaddingRight(int p)
{
padright = p;
sizepanel(true);
invalidate();
}
/**
* Gets the current right border pad amount.
* @return right border pad amount
* @see #setPaddingRight
*/
public int getPaddingRight()
{
return padright;
}
/**
* Sets the top inset border padding amount.
* @param t top inset border pad amount
* @see #getIPadTop
*/
public void setIPadTop(int t)
{
iyPadTop = t;
sizepanel(true);
invalidate();
}
/**
* Sets the bottom inset border padding amount.
* @param b bottom inset border pad amount
* @see #getIPadBottom
*/
public void setIPadBottom(int b)
{
iyPadBottom = b;
sizepanel(true);
invalidate();
}
/**
* Gets the current top inset border padding amount.
* @return top inset border pad amount
* @see #setIPadTop
*/
public int getIPadTop()
{
return iyPadTop;
}
/**
* Sets the side inset border padding amount.
* @param s side inset border pad amount
* @see #getIPadSides
*/
public void setIPadSides(int s)
{
ixPad = s;
sizepanel(true);
invalidate();
}
/**
* Gets the current bottom inset border padding amount.
* @return bottom inset border pad amount
* @see #setIPadBottom
*/
public int getIPadBottom()
{
return iyPadBottom;
}
/**
* Sets the border padding amounts.
* @param t top pad amount
* @param b bottom pad amount
* @param l left pad amount
* @param r right pad amount
*/
public void setPadding(int t, int b, int l, int r)
{
padtop = t;
padbottom = b;
padleft = l;
padright = r;
sizepanel(true);
invalidate();
}
/**
* Gets the current side inset border padding amount.
* @return side inset border pad amount
* @see #setIPadSides
*/
public int getIPadSides()
{
return ixPad;
}
/**
* Sets the current border label.
* @param label border label. If null, label is removed
* @see #getLabel
*/
public void setLabel(String s)
{
label = (s != null && s.length() == 0) ? null : s;
sizepanel(true);
invalidate();
}
/**
* Gets the current border label.
* @see #setLabel
*/
public String getLabel()
{
return label;
}
/**
* Sets the current border color, and optionally the border label color.
* @param clr new border color
* @param useForLabel if true use the new border color for the label color
* as well
* @see #getBorderColor
*/
public void setBorderColor(Color clr, boolean useForLabel)
{
borderColor = clr;
if (useForLabel)
{
labelColor = clr;
}
invalidate();
}
/**
* Sets the current border color.
* @param clr new border color
* @see #getBorderColor
*/
public void setBorderColor(Color clr)
{
setBorderColor(clr, false);
}
/**
* Gets the current border color.
* @return current border color
* @see #setBorderColor
*/
public Color getBorderColor()
{
return borderColor;
}
/**
* Sets the border label alignment.
* @param alignment new border label alignment
* @see #getAlignStyle
* @see AlignStyle#ALIGN_LEFT
* @see AlignStyle#ALIGN_CENTERED
* @see AlignStyle#ALIGN_RIGHT
*/
public void setAlignStyle(int alignment)
{
labelAlignment = alignment;
sizepanel(true);
invalidate();
}
/**
* Gets the current border label alignment.
* @return current border label alignment
* @see AlignStyle#ALIGN_LEFT
* @see AlignStyle#ALIGN_CENTERED
* @see AlignStyle#ALIGN_RIGHT
* @see #setAlignStyle
*/
public int getAlignStyle()
{
return labelAlignment;
}
/**
* Sets the border label color.
* @param clr new border label color
* @see #getLabelColor
*/
public void setLabelColor(Color clr)
{
labelColor = clr;
invalidate();
}
/**
* Gets the current border label color.
* @return current border label color
*/
public Color getLabelColor()
{
return labelColor;
}
/**
* Sets the border style.
* @param s new border style
* @see BevelStyle#BEVEL_RAISED
* @see BevelStyle#BEVEL_LOWERED
* @see BevelStyle#BEVEL_LINE
* @see BevelStyle#BEVEL_NONE
* @see #getBevelStyle
*/
public void setBevelStyle(int s)
{
style = s;
invalidate();
}
/**
* Gets the current border style.
* @return current border style
* @see BevelStyle#BEVEL_RAISED
* @see BevelStyle#BEVEL_LOWERED
* @see BevelStyle#BEVEL_LINE
* @see BevelStyle#BEVEL_NONE
* @see #setBevelStyle
*/
public int getBevelStyle()
{
return style;
}
/**
* Returns the amount of space used by the current border.
* This is a standard Java AWT method which gets called to determine
* the size of the current border. The returned value is the width
* of each border side in pixels.
*
* @return the current border insets
*/
public void setInternalInsets(Insets insets)
{
internalInsets = insets;
sizepanel(true);
invalidate();
}
/**
* Gets the current internal border insets.
* @return current internal border insets
* @see #setInternalInsets
*/
public Insets getInternalInsets()
{
return internalInsets;
}
/**
* Handles the laying out of components within this component.
* This is a standard Java AWT method which gets called by the AWT
* when this component is validated with the validate() method.
*
* @see java.awt.Container#validate
*/
public void layout()
{
sizepanel(false);
panel.layout();
}
/**
* Returns the minimum dimensions to properly display this component.
* This is a standard Java AWT method which gets called to determine
* the minimum size of this component.
*
* @see #preferredSize
*/
public Dimension minimumSize()
{
return preferredSize();
}
/**
* Returns the recommended dimensions to properly display this component.
* This is a standard Java AWT method which gets called to determine
* the recommended size of this component.
*
* @see #minimumSize
*/
public Dimension preferredSize()
{
Dimension s;
s = panel.preferredSize();
s.width = Math.max(s.width, getLabelWidthMargin());
s.width += (padleft + padright + ixPad * 2) + 1;
s.height += (getLabelTopMargin() + padbottom + iyPadTop + iyPadBottom) + 1;
return s;
}
/**
* Sets the layout manager to be used to layout this container.
* This is a standard Java AWT method which gets called to specify
* which layout manager should be used to layout the components in
* standard containers.
*
* @param l the layout manager to use to layout this container's components
* (IGNORED)
* @see #getLayout
**/
public void setLayout(LayoutManager l)
{
if (panel != null)
{
panel.setLayout(l);
}
}
/**
* Gets the current border panel layout manager.
* @return current LayoutManager of border panel
* @see #setLayout
*/
public LayoutManager getLayout()
{
return panel.getLayout();
}
/**
* Adds a component to the end of this container.
* This is a standard Java AWT method which gets called to add a
* component to a container. The specified component is added to
* the end of this container.
*
* @param comp the component to add
* @return the added component
* @see #remove
*/
public Component add(Component c)
{
return panel.add(c);
}
/**
* Adds a component to the end of this container and to the layout manager.
* This is a standard Java AWT method which gets called to add a
* component to a container. The specified component is added to
* the end of this container, and also added to this container's
* layout manager with the given name.
*
* @param name the positioning directive for the layout manager
* @param comp the component to add
* @return the added component
* @see #remove
*/
public Component add(String s, Component c)
{
return panel.add(s, c);
}
/**
* Inserts a component into this container at the given position.
* This is a standard Java AWT method which gets called to add a
* component to a container. The specified component is added to
* this container at the given zero-relative position index. A
* position index of -1 appends the component to the end.
*
* @param comp the component to add
* @param pos the zero-relative index at which to add the component or -1 for end
* @return the added component
* @see #remove
*/
public Component add(Component c, int pos)
{
if (c == panel)
{
return super.add(c, pos);
}
return panel.add(c, pos);
}
/**
* Removes the specified component from this container.
* This is a standard Java AWT method which gets called to remove a
* component from a container. When this happens the component's
* removeNotify() will also get called to indicate component removal.
*
* @param c the component to remove
* @see #removeAll
* @see #add
*/
public void remove(Component c)
{
panel.remove(c);
}
/**
* Removes all the components from this container.
* This is a standard Java AWT method which gets called to remove all
* the components from a container. When this happens each component's
* removeNotify() will also get called to indicate component removal.
*
* @see #remove
* @see #add
*/
public void removeAll()
{
panel.removeAll();
}
/**
* Moves and/or resizes this component.
* This is a standard Java AWT method which gets called to move and/or
* resize this component. Components that are in containers with layout
* managers should not call this method, but rely on the layout manager
* instead.
*
* @param x horizontal position in the parent's coordinate space
* @param y vertical position in the parent's coordinate space
* @param width the new width
* @param height the new height
*/
public void reshape(int x, int y, int width, int height)
{
super.reshape(x, y, width, height);
sizepanel(false);
}
/**
* Handles redrawing of this component on the screen.
* This is a standard Java AWT method which gets called by the Java
* AWT (repaint()) to handle repainting this component on the screen.
* The graphics context clipping region is set to the bounding rectangle
* of this component and its <0,0> coordinate is this component's
* top-left corner.
* Typically this method paints the background color to clear the
* component's drawing space, sets graphics context to be the foreground
* color, and then calls paint() to draw the component.
*
* @param g the graphics context
* @see java.awt.Component#repaint
* @see #paint
*/
public void update(Graphics g)
{
Dimension s;
Insets insets;
s = size();
insets = insets();
g.setColor(getBackground());
if (insets.left > 0)
{
g.fillRect(0, 0, insets.left, s.height);
}
if (insets.top > 0)
{
g.fillRect(0, 0, s.width, insets.top);
}
if (insets.bottom > 0)
{
g.fillRect(0, s.height-insets.bottom, s.width, insets.bottom);
}
if (insets.right > 0)
{
g.fillRect(s.width-insets.right, 0, insets.right, s.height);
}
paint(g);
panel.repaint();
}
/**
* Returns the number of components in this container.
* This is a standard Java AWT method which gets called to return a count
* of the components within this container.
*
*/
public int countComponents()
{
return panel.countComponents();
}
/**
* Gets the component at a specific zero-relative component index.
* This is a standard Java AWT method which gets called to return
* the component at a specific position.
*
* @param i the zero-relative index of the component to retrieve
* @return the component at the given index
* @exception java.lang.ArrayIndexOutOfBoundsException if the given
* component index does not exist
* see getComponents
*/
public Component getComponent(int i)
{
return panel.getComponent(i);
}
/**
* Returns all of the components in this container.
* This is a standard Java AWT method which gets called to return
* an array of all of the components in this container.
*
* @return an array of components in this container
*/
public Component[] getComponents()
{
return panel.getComponents();
}
/**
* Returns the current border insets.
*/
public Insets insets()
{
int h;
Insets insets;
h = getLabelTopMargin();
insets = getInternalInsets();
return new Insets(h + insets.top, insets.left, insets.bottom, insets.right);
}
/**
* Sizes the border panel from the padding, inset, and label margin information.
* @param force force reshape even if the overall size hasn't changed
*/
void sizepanel(boolean force)
{
Dimension s;
s = size();
if (force || oldSize.width != s.width || oldSize.height != s.height)
{
oldSize = size();
panel.reshape(padleft + ixPad,
getLabelTopMargin() + iyPadTop,
oldSize.width - padright - padleft - ixPad * 2 - 1,
oldSize.height - padbottom - getLabelTopMargin() - iyPadBottom - iyPadTop - 1);
}
}
/**
* Paints this component using the given graphics context.
* This is a standard Java AWT method which typically gets called
* by the AWT to handle painting this component. It paints this component
* using the given graphics context. The graphics context clipping region
* is set to the bounding rectangle of this component and its <0,0>
* coordinate is this component's top-left corner.
*
* @param g the graphics context used for painting
* @see java.awt.Component#repaint
* @see #update
*/
public void paint(Graphics g)
{
sizepanel(false);
g.setColor(getBackground());
draw(g);
}
/**
* Draws the border panel.
* @param g current graphics object
*/
void draw(Graphics g)
{
Dimension s;
int delta;
FontMetrics fm;
int x;
int y;
int w;
int h;
s = size();
delta = padtop;
fm = getFontMetrics(getFont());
g.clipRect(0, 0, s.width, s.height);
if (label != null && fm != null)
{
delta = (fm.getAscent() + fm.getDescent() + padtop) / 2;
}
x = padleft;
y = delta;
w = s.width - padleft - padright - 1;
h = s.height - 1 - delta - padbottom;
drawBorder(g, x, y, w, h);
drawLabel(g, fm);
}
/**
* Draws the border panel border.
* @param g current graphics object
* @param x x coordinate of panel object
* @param y y coordinate of panel object
* @param w w width of panel object
* @param h h height of panel object
*/
void drawBorder(Graphics g, int x, int y, int w, int h)
{
switch(style)
{
case BEVEL_RAISED :
{
g.setColor(Color.white);
g.drawLine(x, y, x + w, y);
g.drawLine(x, y, x, y + h);
g.setColor(Color.gray);
g.drawLine(x, y + h, x + w, y + h);
g.drawLine(x + w, y, x + w, y + h);
break;
}
case BEVEL_LOWERED :
{
g.setColor(Color.gray);
g.drawLine(x, y, x + w, y);
g.drawLine(x, y, x, y + h);
g.setColor(Color.white);
g.drawLine(x, y + h, x + w, y + h);
g.drawLine(x + w, y, x + w, y + h);
break;
}
case BEVEL_NONE :
{
break;
}
case BEVEL_LINE :
{
g.setColor(borderColor);
g.drawRect(x, y, w, h);
break;
}
default:
{
g.setColor(borderColor);
g.drawRect(x, y, w, h);
break;
}
}
}
/**
* Draws the border panel label.
* @param g current graphics object
* @param fm FontMetrics of border panel label
*/
void drawLabel(Graphics g, FontMetrics fm)
{
if (label != null && (fm != null))
{
int fWidth;
Dimension s;
int stringWidth;
int ascent;
int descent;
int x;
int y;
int h;
fWidth = 10;
s = size();
if (getFont().getSize() > fWidth)
{
fWidth = fWidth + getFont().getSize() / 2;
}
stringWidth = fm.stringWidth(label);
ascent = fm.getAscent();
descent = fm.getDescent();
switch(labelAlignment)
{
case Label.CENTER:
{
x = (s.width - stringWidth) / 2;
break;
}
case Label.RIGHT:
{
x = s.width - fWidth - (stringWidth + (labelpadx + labelipadx) / 2);
break;
}
case Label.LEFT:
{
}
default:
{
x = fWidth + (labelpadx + labelipadx) / 2;
}
}
//y = ascent + padtop; //This line is useless. 12/17/96 Levi Brown
h = ascent + descent+padtop;
y = (fWidth - h) / 2 + (padtop + ascent);
//h = fWidth; //I like h better before... 12/17/96 Levi Brown
g.setColor(getBackground());
//Commented out the line below, to add my own below it. 12/17/96 Levi Brown
//g.fillRect(x - labelipadx / 2, 0, stringWidth + labelipadx, h);
g.fillRect(x - labelipadx / 2, y - 1 - ascent - padtop/2, stringWidth + labelipadx, h);
g.setColor(labelColor);
g.drawString(label, x, y - 1);
}
}
/**
* Returns the current label top margin.
*/
protected int getLabelTopMargin()
{
int top;
Font font;
if (label == null)
{
return padtop;
}
top = padtop;
font = getFont();
if (font != null)
{
FontMetrics fm;
fm = getFontMetrics(font);
top = fm.getAscent() + fm.getDescent() + padtop + 0;
}
return top;
}
/**
* Returns the current label width margin.
*/
int getLabelWidthMargin()
{
if (label == null)
{
return 0;
}
int w;
Font font;
w = 2 + internalInsets.left + internalInsets.right;
font = getFont();
if (font != null)
{
FontMetrics fm;
fm = getFontMetrics(font);
w = Math.max(w, 2 + fm.stringWidth(label) + labelpadx + labelipadx);
}
return w;
}
/**
* Sets this component's background color.
* This is a standard Java AWT method which gets called to change
* the background color of this component.
*
* @param c the new background color
* @see java.awt.Component#getBackground
*/
public void setBackground(Color c)
{
super.setBackground(c);
panel.setBackground(c);
}
}